import axios from "axios";

// eslint-disable-next-line no-undef
var backendUrl = loginUrl;

export const setUrl = (url) => {
    // eslint-disable-next-line no-undef
    backendUrl = url;
};

/**
 * 保存或修改sn或手机号
 * @param {*} openId
 * @param {*} sn
 * @param {*} phone
 */
export const updateUserInfo = (
    openId,
    sn = "",
    phone = "",
    resetPhone = false
) => {
    // eslint-disable-next-line no-undef
    const param = { openId: openId };
    if (phone) {
        param["phone"] = phone;
    }
    if (sn) {
        param["sn"] = sn;
    }
    if (resetPhone) {
        param["phone"] = "";
    }
    return axios.post(backendUrl + "/ivcs/mindary/customer", param, {
        headers: {
            "x-access-token": localStorage.getItem("mindary_token"),
        },
    });
};

/**
 * 获取sn和手机号
 * @param {*} openId
 */
export const getUserInfo = (openId) => {
    return axios.get(loginUrl + `/ivcs/mindary/customer/${openId}`, {
        headers: {
            "x-access-token": localStorage.getItem("mindary_token"),
        },
    });
};

/**
 * 获取设备信息
 * @param {*} sn
 */
export const getDeviceInfo = (sn) => {
    return axios.get(loginUrl + `/ivcs/mindary/device/${sn}`, {
        headers: {
            "x-access-token": localStorage.getItem("mindary_token"),
        },
    });
};

/**
 * 创建工单
 * @param {*} data {openId, sn, phone, ZFWZL_CODE, XCX_ID} ZFWZL_CODE: 001=维修|007=临床服务, XCX_ID: sessionId
 */
export const createTicket = (data) => {
    // eslint-disable-next-line no-undef
    return axios.post(backendUrl + "/ivcs/mindary/workOrder", data, {
        headers: {
            "x-access-token": localStorage.getItem("mindary_token"),
        },
    });
};

/**
 * 提交设备问题
 * @param {*} id 工单id
 * @param {*} data {phone, name, content}
 */
export const submitQuestion = (id, data) => {
    // eslint-disable-next-line no-undef
    return axios.post(
        backendUrl + `/ivcs/mindary/workOrder/${id}/question`,
        data,
        {
            headers: {
                "x-access-token": localStorage.getItem("mindary_token"),
            },
        }
    );
};

/**
 * 获取工单列表
 * @param {*} openId
 * @param {*} pageSize
 * @param {*} pageNum
 */
export const getTicketList = (openId) => {
    return axios.get(loginUrl + `/ivcs/mindary/workOrder/list/${openId}`, {
        headers: {
            "x-access-token": localStorage.getItem("mindary_token"),
        },
    });
};

/**
 * 通话详情查看
 * @param {*} id
 */
export const getTalkInfo = (id) => {
    return axios.get(loginUrl + `/ivcs/mindary/workOrder/${id}/session/`, {
        headers: {
            "x-access-token": localStorage.getItem("mindary_token"),
        },
    });
};

/**
 * 查询工单详情
 * @param {*} id 工单号
 */
export const getTicketDetail = (id) => {
    return axios.get(loginUrl + `/ivcs/mindary/workOrder/detail/${id}`, {
        headers: {
            "x-access-token": localStorage.getItem("mindary_token"),
        },
    });
};

/**
 * 游客登录
 * @param {*} id
 * @param {*} data {openId, org}
 */
export const login = (openId, org) => {
    // eslint-disable-next-line no-undef
    const data = { openId: openId, org: org };
    return axios.post(backendUrl + "/ivcs/api/v1/auth/register/openid", data, {
        headers: {},
    });
};

/**
 * 获取场景列表
 */
export const getSceneList = () => {
    return axios.get(
        loginUrl + "/ivcs/api/v1/bizscenarios/select/options?status=enabled",
        {
            headers: {
                "x-access-token": localStorage.getItem("mindary_token"),
            },
        }
    );
};

/**
 * 建立会话
 * @param {*} sessionId
 */
export const updateSessionId = (ticketId, sessionId) => {
    // eslint-disable-next-line no-undef
    const data = { sessionId: sessionId };
    return axios.post(
        backendUrl + `/ivcs/mindary/workOrder/${ticketId}/session`,
        data,
        {
            headers: {
                "x-access-token": localStorage.getItem("mindary_token"),
            },
        }
    );
};

/**
 * 访客注销
 * @param {*} openId
 */
// https://localhost/ivcs/mindary/customer
export const logout = (openId) => {
    const data = { openId: openId };
    return axios.delete(backendUrl + "/ivcs/mindary/customer", {
        data: data,
        headers: {
            "x-access-token": localStorage.getItem("mindary_token"),
        },
    });
};

/**
 * 获取工作时间
 * @param {*} sceneId
 */
export const getWorkTime = (sceneId) => {
    return axios.get(loginUrl + `/ivcs/mindary/workTime/${sceneId}`, {
        headers: {
            "x-access-token": localStorage.getItem("mindary_token"),
        },
    });
};

/**
 * 发送短信
 * @param {*} phone
 * @param {*} content
 * @param {*} scenId 业务场景id
 */
export const sendMoblieMessage = (phone, content, scenId) => {
    // eslint-disable-next-line no-undef
    const data = { phone: phone, content: content, scenId: scenId };
    return axios.post(backendUrl + `/ivcs/mindary/sendMsg`, data, {
        headers: {
            "x-access-token": localStorage.getItem("mindary_token"),
        },
    });
};

/**
 * 获取场景下可用坐席
 * @param {*} scenId
 * @returns
 */
export const getAvailableAgent = (scenId) => {
    return axios.get(
        loginUrl + `/ivcs/api/v1/bizscenarios/${scenId}/available/agent`,
        {
            headers: {
                "x-access-token": localStorage.getItem("mindary_token"),
            },
        }
    );
};

/**
 * 扫描仪器生成的测试量二维码，并调用iService平台提供的测试量接口，将二维码中的加密信息回传到iService平台
 * @param {string} openId 工单id
 * @param {string} qrCode 二维码
 * @param {string} userId 用户id
 */
export const submitQuantityScanCode = (openId, qrCode, userId) => {
    // eslint-disable-next-line no-undef
    return axios.post(
        backendUrl + `/ivcs/mindary/TTesting/quantityScanCode`,
        { openId, qrCode, userId },
        {
            headers: {
                "x-access-token": localStorage.getItem("mindary_token"),
            },
        }
    );
};
